﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class DienBienLuongController : Controller
    {
        //
        // GET: /DienBienLuong/

        public ActionResult Index(int id =0)
        {
            CanBo cb = CanBoModels.GetChiTietCanBo(id);
            ChucVu cv = PosModels.GetChiTietDSChucVu((int)cb.ChucVuHienTai);
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(cb.IDPhongBan);
            ViewBag.ChucVu = cv;
            ViewBag.PhongBan = pb;
            ViewBag.ChiTietCanBo = cb;
            return View();
        }

        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dbl in context.tblDienBienLuongs
                            from chucvu in context.DSChucVu
                            from mangach in context.DSMaNgach
                            where dbl.IDCanBo == id && dbl.IDChucVu == chucvu.ID && dbl.IDMaNgach == mangach.ID
                            select new {dbl.ID,dbl.TuNgay,dbl.DenNgay,TenChucVu=chucvu.Ten,mangach.TenNgach,dbl.HeSoLuong,dbl.BacLuong,dbl.NgayTangLuong }).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.TuNgay,
                                 customer.DenNgay,
                                 customer.TenChucVu,
                                 customer.TenNgach,
                                 customer.HeSoLuong,
                                 customer.BacLuong,
                                 customer.NgayTangLuong
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }

    }
}
